// 部署配置
const deployConfig = {
  // 开发环境配置
  development: {
    name: '开发环境',
    basePath: '/',
    publicPath: '/',
    outputDir: 'dist',
    assetsDir: 'assets',
    filenameHashing: true,
    sourcemap: true,
    minify: false,
    chunkSizeWarningLimit: 500,
    css: {
      extract: false,
      sourceMap: true,
    },
  },
  
  // 测试环境配置
  test: {
    name: '测试环境',
    basePath: '/',
    publicPath: '/movie-app/',
    outputDir: 'dist-test',
    assetsDir: 'assets',
    filenameHashing: true,
    sourcemap: true,
    minify: true,
    chunkSizeWarningLimit: 1000,
    css: {
      extract: true,
      sourceMap: false,
    },
    // 测试环境特定配置
    testConfig: {
      apiCheck: true,
      performanceTest: true,
      securityScan: true,
    },
  },
  
  // 生产环境配置
  production: {
    name: '生产环境',
    basePath: '/',
    publicPath: 'https://cdn.example.com/movie-app/',
    outputDir: 'dist-prod',
    assetsDir: 'assets',
    filenameHashing: true,
    sourcemap: false,
    minify: true,
    chunkSizeWarningLimit: 2000,
    css: {
      extract: true,
      sourceMap: false,
    },
    // 生产环境优化配置
    optimization: {
      splitChunks: {
        chunks: 'all',
        cacheGroups: {
          vendor: {
            name: 'chunk-vendors',
            test: /[\\\\/]node_modules[\\\\/]/,
            priority: 10,
            chunks: 'initial',
          },
          common: {
            name: 'chunk-common',
            minChunks: 2,
            priority: 5,
            chunks: 'initial',
          },
        },
      },
    },
    // 生产环境特定配置
    prodConfig: {
      gzip: true,
      brotli: true,
      cdn: true,
      monitoring: true,
    },
  },
};

// 根据环境获取配置
function getConfig(env = process.env.NODE_ENV || 'development') {
  return deployConfig[env] || deployConfig.development;
}

// 构建配置生成器
function generateBuildConfig(env) {
  const config = getConfig(env);
  
  return {
    // 基础路径
    base: config.basePath,
    
    // 构建输出目录
    build: {
      outDir: config.outputDir,
      assetsDir: config.assetsDir,
      sourcemap: config.sourcemap,
      minify: config.minify,
      chunkSizeWarningLimit: config.chunkSizeWarningLimit,
      rollupOptions: config.optimization || {},
    },
    
    // 静态资源处理
    publicDir: 'public',
    
    // 开发服务器配置
    server: {
      port: 5174,
      host: true,
      proxy: {
        '/api': {
          target: env === 'production' 
            ? 'https://api.example.com' 
            : 'http://localhost:8081',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
    
    // CSS配置
    css: config.css,
    
    // 插件配置
    plugins: [],
  };
}

module.exports = {
  deployConfig,
  getConfig,
  generateBuildConfig,
};

// 命令行使用
if (require.main === module) {
  const env = process.argv[2] || 'development';
  console.log(`当前环境: ${env}`);
  console.log(JSON.stringify(getConfig(env), null, 2));
}